//
//  XGBseTpoicTableViewCell.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/23.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "XGBaseTopicTableViewCell.h"
#import "XGTopicCellTopView.h"
#import "XGTopicCellBottomView.h"
#import "XGTopicCellTopCommentView.h"

#import "XGTopicModel.h"

@implementation XGBaseTopicTableViewCell

// readonly属性实现getter方法不会自动生成成员变量 使用@synthesize手动生成
@synthesize topView = _topView;
@synthesize contentLabel = _contentLabel;
@synthesize bottomView = _bottomView;
@synthesize topCommentView = _topCommentView;

#pragma mark - 解析模型

- (void)setTopicModel:(XGTopicModel *)topicModel
{
    _topicModel = topicModel;
    
    self.topView.topicModel = topicModel;
    self.contentLabel.text = topicModel.text;
    self.bottomView.topicModel = topicModel;
    
    if (topicModel.topCmt == nil) {
        self.topCommentView.hidden = YES;
    } else {
        // 有最热评论
        self.topCommentView.topCommentModel = topicModel.topCmt;
        self.topCommentView.hidden = NO;
    }
}

#pragma mark - 构造方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)setUpUI
{
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageWithImageName:@"mainCellBackground"]];
                                        
    // 添加子控件
    [self.contentView addSubview:backgroundImageView];
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.bottomView];
    [self.contentView addSubview:self.topCommentView];
 
    // 自动布局
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 1, 0, 1));
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(kTopCellTopViewHeight);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(kMargin);
        make.left.equalTo(self.contentView).offset(kMargin);
        make.right.equalTo(self.contentView).offset(-kMargin);
    }];
    
    [self.topCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kMargin);
        make.right.equalTo(self.contentView).offset(-kMargin);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(kTopCellBottomViewHeight);
    }];
}

#pragma mark - 懒加载

- (XGTopicCellTopView *)topView
{
    if (_topView == nil) {
        _topView = [[XGTopicCellTopView alloc] init];
        _topView.backgroundColor = [UIColor clearColor];
    }
    
    return _topView;
}

- (UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel labelWithText:@"这是测试文本数据~" textColor:[UIColor lightGrayColor] font:15 textAlignment:NSTextAlignmentLeft];
    }
    
    return _contentLabel;
}

- (XGTopicCellBottomView *)bottomView
{
    if (_bottomView == nil) {
        _bottomView = [[XGTopicCellBottomView alloc] init];
        _bottomView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
    }
    
    return _bottomView;
}

- (XGTopicCellTopCommentView *)topCommentView
{
    if (_topCommentView == nil) {
        _topCommentView = [[XGTopicCellTopCommentView alloc] init];
        _topCommentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.4];
    }
    
    return _topCommentView;
}

@end
